package net.tngou.tpage.action.my;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;

import net.tngou.tpage.action.BaseAction;
import net.tngou.tpage.jdbc.OrderType;
import net.tngou.tpage.pojo.Attribute;
import net.tngou.tpage.pojo.Template;

/**
 * 模板
 * @author tngou@tngou.net
 *
 */
public class TemplateAction extends BaseAction {


	/**
	 * 公共模板
	 */
	@Override
	public void execute() throws ServletException, IOException {
		
		printFreemarker("my/template.ftl", root);
	}
	
	public void add() {
		printFreemarker("my/template_add.ftl", root);
	}
	
	/**
	 * 私有模板
	 */
	public void my() {
		printFreemarker("my/template_my.ftl", root);
	}
	
	/**
	 * 模板编辑
	 */
	public void html() {
		if(ask.getId()==0)
		{
			run_404();
		}
		Template template = new Template().get(ask.getId());
		if(template==null||!((template.getUser()==0&&user.getRole()==1)||template.getUser()==user.getId())){
			run_500();
		}
		List<?> attributes = service.getList("seq", OrderType.ASC, Attribute.class);
		root.put("attributes", attributes);
		
		root.put("template", template);
		printFreemarker("my/template_html.ftl", root);		
	}
	
	
	/**
	 * 模板预览
	 */
	public void show() {
		if(ask.getId()==0)
		{
			run_404();
		}
		Template template = new Template().get(ask.getId());
		if(template==null||!((template.getUser()==0&&user.getRole()==0)||template.getUser()==user.getId())){
			run_500();
		}
		root.put("template", template);
		printFreemarker("my/template_show.ftl", root);
	}
}
